package com.bangdream.michelia.view.fragment.main.task;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.andview.refreshview.XRefreshView;
import com.bangdream.michelia.R;
import com.bangdream.michelia.contract.ExamContract;
import com.bangdream.michelia.entity.RxEventBean;
import com.bangdream.michelia.entity.courses.CoursesBean;
import com.bangdream.michelia.entity.exam.ExamBean;
import com.bangdream.michelia.entity.exam.ExamQuestBean;
import com.bangdream.michelia.entity.exam.ExamResultDetailsBean;
import com.bangdream.michelia.entity.exam.ExamResultItemBean;
import com.bangdream.michelia.entity.exam.SubjectBean;
import com.bangdream.michelia.entity.exam.SubjectInfoBean;
import com.bangdream.michelia.presenter.ExamPresenter;
import com.bangdream.michelia.utils.PublicResources;
import com.bangdream.michelia.utils.RxBus;
import com.bangdream.michelia.utils.ToastUtil;
import com.bangdream.michelia.view.activity.questionnaire.QuestionnaireActivity;
import com.bangdream.michelia.view.adapter.ExaminationAdapter;
import com.bangdream.michelia.view.adapter.TaskQuListAdapter;
import com.bangdream.michelia.view.adapter.TaskQuestListAdapter;
import com.bangdream.michelia.view.fragment.currency.LazyLoadFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * @date on 2018/6/18 17:13
 * @email 909732184@qq.com
 * 任务-问卷
 */
public class TaskQuestQueryList extends LazyLoadFragment<ExamContract.IExamView,ExamPresenter<ExamContract.IExamView>>
        implements ExamContract.IExamView,ExaminationAdapter.OnItemClickListener{
    private View view;
    //刷新加载控件
    private XRefreshView refreshView;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private TaskQuListAdapter adapter;
    private int page = 1;
    private String type;
    private List<ExamBean> dataList;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.recycler_view_layout,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        setListener();
        operateBus();
    }

    private void setListener() {
        refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener(){
            @Override
            public void onRefresh(boolean isPullDown) {
                super.onRefresh(isPullDown);
                page = 1;
                loadData();
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                page++;
                loadData();
            }
        });

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter a, View view, int position) {
                if(adapter.getData().get(position).getCanDuplication().equals("1")){
                    Intent intent = new Intent(getActivity(), QuestionnaireActivity.class);
                    intent.putExtra("id",adapter.getData().get(position).getId());
                    intent.putExtra("paperId",adapter.getData().get(position).getExamId());
                    intent.putExtra("title",adapter.getData().get(position).getTitle());
//                intent.putExtra("data",adapter.getData().get(position));
//                startActivityForResult(intent,ExamInfoCode);
                    startActivity(intent);
                }else{
                    ToastUtil.showShortToast(mApp,"问卷无法重复填写");
                }


            }
        });

    }


    @Override
    public ExamPresenter<ExamContract.IExamView> createPresenter() {
        return new ExamPresenter();
    }

    @Override
    public void loadData() {
//        adapter.setEmptyView(loadingView);
        type= "2";
        mPresenter.getExamQuList(type,null);
    }

    private void initView() {
        if(getArguments()!=null){
            type = getArguments().getString("type");
        }
        refreshView = view.findViewById(R.id.refreshView);
        refreshView.setPullRefreshEnable(true);
        refreshView.setPullLoadEnable(false);
        recyclerView = view.findViewById(R.id.recyclerView);
        initEmptyView(recyclerView);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
//        adapter = new TaskQuListAdapter(getActivity());main_task_quest_adapter_item
        adapter = new TaskQuListAdapter(R.layout.main_task_quest_adapter_item);
        recyclerView.setAdapter(adapter);
    }


    @Override
    public void setExamList(boolean isSuccess, int page, List<ExamBean> data, String stDescribe) {
        if(isSuccess&&data!=null) {
        }
    }

    @Override
    public void setSubjectList(boolean isSuccess, List<SubjectBean> data, String stDescribe) {

    }

    @Override
    public void setSubjectDetails(boolean isSuccess, SubjectInfoBean data, String stDescribe) {

    }

    @Override
    public void initExamId(boolean isSuccess, String examId, String stDescribe) {

    }

    @Override
    public void actionSubmitExam(boolean isSuccess, boolean isFinish, String stDescribe) {

    }

    @Override
    public void actionSubmitQuest(boolean isSuccess, boolean isFinish, String stDescribe) {

    }

    @Override
    public void setExamResultList(boolean isSuccess, List<ExamResultItemBean> data, String stDescribe) {

    }

    @Override
    public void setExamResultDetails(boolean isSuccess, ExamResultDetailsBean data, String stDescribe) {

    }

    @Override
    public void setExamQuestList(boolean isSuccess, List<ExamQuestBean> data, String stDescribe) {

    }

    @Override
    public void setExamQuList(boolean isSuccess, List<ExamBean> data, String stDescribe) {

//            dataList = data;
//            adapter.setData(dataList);
//            adapter.notifyDataSetChanged();
//            refreshView.stopRefresh();
            if (data!=null){
                if(page==1) {
                    adapter.setNewData(data);
                    adapter.setData(data);
                    adapter.notifyDataSetChanged();
                    refreshView.stopRefresh();
                    //显示数据大于20条才允许上拉加载更多
                    if(data.size()>=PublicResources.PageSize){
                        refreshView.setPullLoadEnable(true);
                    }
                    if(data.size()==0){
                        adapter.setEmptyView(noDataView);
                    }
                }else {
                    adapter.getData().clear();
                    adapter.addData(data);
                    refreshView.stopLoadMore();
                }
            }else {
                //如果是加载新数据时失败了 则page-1
                if (page>1){
                    refreshView.stopLoadMore();
                    this.page--;
                }else {
                    //还是还原为1防止出现负数
                    this.page = 1;
                    refreshView.stopRefresh();
                    adapter.setNewData(null);
                    if(data==null){
                        adapter.setEmptyView(noDataView);
                    }else
                        adapter.setEmptyView(errorView);
                }
            }
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @SuppressLint("CheckResult")
    public void operateBus() {
        disposable = RxBus.getInstance().toObservable().map(new Function<Object, RxEventBean>() {
            @Override
            public RxEventBean apply(Object o) throws Exception {
                return (RxEventBean) o;
            }
        }).subscribe(new Consumer<RxEventBean>() {
            @Override
            public void accept(RxEventBean bean) throws Exception {

                if(bean.getCode()==0x02){
                    refreshView.startRefresh();
                }
            }
        });
    }
}
